<?php
/**
 * PasswordForgotForm AR class file for users table.
 * ------------------------------------------
 *
 * LICENSE: BSD
 * @copyright  2009 Gemisoft
 * @author Deepak Pradhan <deepak.pradhan@gemisoft.com>
 * @link http://grbac.gemisoft.com/
 * @version $Id$
 * @since 0.1
 */

class PasswordForgotForm extends CActiveRecord {

  public $passwordNew_repeat = '';
  public $passwordNew = '';
  public $rememberMe;
  public $captcha;

  /**
   * Returns the static model of the specified AR class.
   * @return CActiveRecord the static model class
   */
  public static function model($className=__CLASS__) {
    return parent::model($className);
  }

  /**
   * @return string the associated database table name
   */
  public function tableName() {
    return 'users';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules() {
    return array(
    array('username','length','max'=>45),
    array('passwordNew','length','max'=>45, 'min'=>6),
    array('passwordNew_repeat','length','max'=>45, 'min'=>6),
    array('email','length','max'=>255),
    array('email','CEmailValidator'),
    array('passwordNew', 'compare', 'message'=>'Passwords do not match'),
    array('active', 'numerical', 'integerOnly'=>true),

    // on update check only username and email:
    array('username, email', 'required', 'on'=>'update'),

    //on insert, update and register:
    array('email, username', 'unique', 'on'=>array('update')),

    //on forgot and resetPassword
    array('captcha', 'captcha', 'allowEmpty'=>!extension_loaded('gd'), 'on'=>array('forgot')),

    //on forgot and resetPassword
    //array('captcha', 'captcha', 'allowEmpty'=>!extension_loaded('gd'), 'on'=>array('resetPassword')),

    //on forgot lookup
    array('username, email', 'validateForgot', 'on'=>'forgot'),

    //on reset password form
    array('passwordNew, passwordNew_repeat', 'required', 'on'=>array('update', 'resetPassword')),
    );
  }

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels() {
    return array(
    'passwordNew_repeat' => 'Repeat password',
    'passwordNew' => 'Password',
    'created' => 'Created',
    'date_lastlogin' => 'Last login',
    'rememberMe'=>'Remember me next time',
    'captcha' => 'Verification code',
    );
  }

  public function safeAttributes() {
    return array(
    'username, email, passwordNew, passwordNew_repeat, active',
    'login' => 'username, password, rememberMe',
    'register' => 'username, email, passwordNew, passwordNew_repeat, captcha',
    'forgot' => 'username, email, captcha',
    'resetPassword' => 'passwordNew, passwordNew_repeat, captcha'
    );
  }

  public function validateForgot($attribute, $params) {
    if(!$this->hasErrors()) {
      $criteria = new CDbCriteria;
      $condition = array();
      if(!empty($this->username)) {
        $condition[] = 'username='.Yii::app()->db->quoteValue($this->username);
      }
      if(!empty($this->email)) {
        $condition[] = 'email='.Yii::app()->db->quoteValue($this->email);
      }
      if(empty($condition)) {
        $this->addError('username','Either username or email should be given');
        $this->addError('email','');
        return;
      }
      $condition[] = 'active=1';
      $criteria->condition = join($condition, ' AND '); //not OR, but AND!
      $user = $this->find($criteria);
      if($user == null) {
        $this->addError('username','Such login was not found');
        $this->addError('email','');
      } else {
        $this->setAttributes($user->getAttributes()); //this will only set safe attributes! but anyway we only need username and email
        $this->valid_code = rand().rand().rand().rand();
        $user->valid_code = $this->valid_code;
        $user->saveAttributes(array('valid_code'));
      }
    }
  }
}